﻿@model Nutrapp.Models.RecipeModel

@{
    ViewBag.Title = "Edit";
    //HtmlHelper.ClientValidationEnabled = false;  //if you don't want client side validation, set both to true
    //HtmlHelper.UnobtrusiveJavaScriptEnabled = false;

}

<div class="panel">
    <div class="panel-heading text-center">
    </div>

    <div class="panel-body">
        @using (Html.BeginForm("Edit", "Recipe", FormMethod.Post, new { enctype = "multipart/form-data" }))
        { @Html.ValidationSummary(true)
            <fieldset>
                @Html.HiddenFor(m => m.ID)
                @foreach (var property in ViewData.ModelMetadata.Properties)
                {
                    if (property.PropertyName != "ID")
                    {
                        <div class="form-group">
                            <label>@(property.DisplayName ?? property.PropertyName)</label>
                            @if (property.PropertyName == "Instruction")
                            {
                                @Html.TextArea(property.PropertyName, null, new { @class = "form-control", rows = 8 })
                            }
                            else if (property.PropertyName == "IngredientLines")
                            {
                                <div id="itemRows">
                                    @if (Model.ID == 0) //create recipe
                                    {
                                        <p>
                                            Ingredient:
                                            <select name="ingr" id="ingr-head-select" class="form-control" style="width: 20%; display: initial">
                                                @foreach (var item in Model.getIngredientList())
                                                {
                                                    <option value="@item.ID">@item.Name</option>
                                                }
                                            </select>
                                            Quantity: <input type="number" min="0" name="quant" class="form-control" style="width: 20%; display: initial" />
                                            Unit:
                                            <select name="unit" id="unit-head-select" class="form-control" style="width: 20%; display: initial">
                                                @foreach (var item in Model.getUnitList())
                                                {
                                                    <option value="@item.ID">@item.Unit1</option>
                                                }
                                            </select>
                                            <input class="btn btn-success" onclick="addRow(this.form);" type="button" value="Add row" />
                                        </p>
                                    }
                                    else //edit recipe
                                    {
                                        <p>Add a new row <input class="btn btn-success" onclick="addRow(this.form)" type="button" value="Add row" /></p>

                                        <select id="ingr-head-select" style="display: none">
                                            @foreach (var i in Model.getIngredientList())
                                            {
                                                <option value="@i.ID">@i.Name</option>
                                            }
                                        </select>

                                        <select id="unit-head-select" style="display: none">
                                            @foreach (var i in Model.getUnitList())
                                            {
                                                <option value="@i.ID">@i.Unit1</option>
                                            }
                                        </select>

                                            var c = 0;
                                            foreach (var item in Model.IngredientLines)
                                            {
                                                c++;

                                                <p id="rowNum-@c">
                                                    Ingredient:
                                                    <select name="ingr" id="ingr-select-@c" class="form-control" style="width: 20%; display: initial">
                                                        @foreach (var i in Model.getIngredientList())
                                                        {
                                                            if (i.Name.Equals(item.Ingredient.Name))
                                                            {
                                                                <option value="@i.ID" selected="selected">@i.Name</option>
                                                            }
                                                            else
                                                            {
                                                                <option value="@i.ID">@i.Name</option>
                                                            }
                                                        }
                                                    </select>
                                                    Quantity: <input type="number" min="0" name="quant" value="@item.Quantity" class="form-control" style="width: 20%; display: initial" />
                                                    Unit:
                                                    <select name="unit" id="unit-select-@c" class="form-control" style="width: 20%; display: initial">
                                                        @foreach (var i in Model.getUnitList())
                                                        {
                                                            if (i.ID == item.UID)
                                                            {
                                                                <option value="@i.ID" selected="selected">@i.Unit1</option>
                                                            }
                                                            else
                                                            {
                                                                <option value="@i.ID">@i.Unit1</option>
                                                            }
                                                        }
                                                    </select>
                                                    <input class="btn btn-success" type="button" value="Remove" onclick="removeRow(@c);">
                                                </p>
                                            }
                                    }
                                </div>
                            }
                            else if (property.PropertyName == "Picture")
                            {
                                <input id="image" name="image" type="file" />
                                if (Model.ID == 0)
                                {
                                    Html.ValidationMessageFor(m => m.Picture);
                                }
                                else 
                                {
                                    <img src="~/Upload/RecipeImage/@Model.Picture" alt="" style="height: 120px; width: 150px" />
                                }
                            }
                            else
                            
                            {
                                @Html.TextBox(property.PropertyName, null, new { @class = "form-control" })
                            }
                            @Html.ValidationMessage(property.PropertyName)
                        </div>

                    }
                }
            </fieldset>
            <div class="panel-footer text-center">
                <button type="submit" class="btn btn-primary">Save</button>
                @Html.ActionLink("Cancel", "Index", "Recipe", null, new { @class = "btn btn-default" })
            </div>
        }
    </div>
</div>
@*<div class="form-group">
        <label class="span2">Recipe Name</label>
        @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
    </div>
    <div class="form-group">
        <label class="span2">Cost</label>
        @Html.TextBoxFor(m => m.Cost, new { @class = "form-control" })
    </div>
    <div class="form-group">
        <label class="span2">Picture</label>
        <input id="File" name="Picture" type="File" value="">
    </div>
    <div class="form-group">
        <label class="span2">Instructions</label>
        @Html.TextBoxFor(m => m.Instruction, new { @class = "form-control" })
    </div>
    <div class="text-center">
        <div class="form-actions">
            <button type="submit" class="btn btn-primary">Save</button>
            @Html.ActionLink("Cancel", "Index", "Recipe", null, new { @class = "btn" })
        </div>
    </div>*@


